package com.byfan.barter.service.Impl;

import com.byfan.barter.entity.Orders;
import com.byfan.barter.jpa.OrdersJpa;
import com.byfan.barter.service.OrdersService;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Author: FBY
 * @Description
 * @Version 1.0
 * @Date: 2021/5/14 16:43
 */
@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersJpa ordersJpa;

    /**
     * 新增/修改一个订单
     * @param orders
     * @return
     */
    @Override
    public Orders save(Orders orders) {
        return ordersJpa.save(orders);
    }

    /**
     * 根据订单id获取订单信息
     * @param id
     * @return
     */
    @Override
    public Orders getById(Integer id) {
        Optional<Orders> optional = ordersJpa.findById(id);
        if (optional.isPresent()){
            return optional.get();
        }
        return null;
    }

    /**
     * 根据用户openId获取所有订单，包括我借入的和借出的
     * @param openId
     * @return
     */
    @Override
    public List<Orders> getByOpenId(String openId) {
        List<Orders> fromList = ordersJpa.findByFromOpenIdOrderByCreatTimeDesc(openId);
        List<Orders> toList = ordersJpa.findByToOpenIdOrderByCreatTimeDesc(openId);
        List<Orders> result = new ArrayList<>();
        int i=0,j=0;
        while (i<fromList.size() && j<toList.size()){
            if (fromList.get(i).getCreatTime().after(toList.get(j).getCreatTime())){
                result.add(fromList.get(i));
                i++;
            }else {
                result.add(toList.get(j));
                j++;
            }
        }
        while (i< fromList.size()){
            result.add(fromList.get(i));
            i++;
        }
        while (j< toList.size()){
            result.add(toList.get(j));
            j++;
        }
        return result.isEmpty()?null:result;
    }

    /**
     * 根据状态获取所有订单，包括我借入的和借出的
     * @param openId
     * @param status
     * @return
     */
    @Override
    public List<Orders> getByStatus(String openId, String status) {
        List<Orders> fromList = ordersJpa.findByFromOpenIdAndStatusOrderByCreatTimeDesc(openId,status);
        List<Orders> toList = ordersJpa.findByToOpenIdAndStatusOrderByCreatTimeDesc(openId,status);
        List<Orders> result = new ArrayList<>();
        int i=0,j=0;
        while (i<fromList.size() && j<toList.size()){
            if (fromList.get(i).getCreatTime().after(toList.get(j).getCreatTime())){
                result.add(fromList.get(i));
                i++;
            }else {
                result.add(toList.get(j));
                j++;
            }
        }
        while (i< fromList.size()){
            result.add(fromList.get(i));
            i++;
        }
        while (j< toList.size()){
            result.add(toList.get(j));
            j++;
        }
        return result.isEmpty()?null:result;
    }


    /**
     * 根据状态查看我借出的
     * @param fromOpenId
     * @param status
     * @return
     */
    @Override
    public List<Orders> getByFromIdAndStatus(String fromOpenId, String status) {
        List<Orders> result = ordersJpa.findByFromOpenIdAndStatusOrderByCreatTimeDesc(fromOpenId,status);
        return result.isEmpty()?null:result;
    }

    /**
     * 根据状态获取我借入的
     * @param toOpenId
     * @param status
     * @return
     */
    @Override
    public List<Orders> getByToIdAndStatus(String toOpenId, String status) {
        List<Orders> result = ordersJpa.findByToOpenIdAndStatusOrderByCreatTimeDesc(toOpenId,status);
        return result.isEmpty()?null:result;
    }

    /**
     * 获取所有我借出的
     * @param fromOpenId
     * @return
     */
    @Override
    public List<Orders> getByFromId(String fromOpenId) {
        List<Orders> result = ordersJpa.findByFromOpenIdOrderByCreatTimeDesc(fromOpenId);
        return result.isEmpty()?null:result;
    }

    /**
     * 获取所有我借入的
     * @param toOpenId
     * @return
     */
    @Override
    public List<Orders> getByToId(String toOpenId) {
        List<Orders> result = ordersJpa.findByToOpenIdOrderByCreatTimeDesc(toOpenId);
        return result.isEmpty()?null:result;
    }

    /**
     * 获取所有剩余时间大于等于0的订单
     * @return
     */
    @Override
    public List<Orders> getAll() {
        List<Orders> result = ordersJpa.findAllByRemainingTimeGreaterThanEqual(0);
        return result.isEmpty()?null:result;
    }


}
